import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_novel/base/base_grid_view.dart';
import 'package:flutter_novel/base/base_stateful_page.dart';
import 'package:flutter_novel/base/empty_widget.dart';
import 'package:flutter_novel/detail/widget/indication_widget.dart';
import 'package:flutter_novel/series_list/bloc/series_list_bloc.dart';
import 'package:flutter_novel/series_list/bloc/series_list_event.dart';
import 'package:flutter_novel/series_list/bloc/series_list_state.dart';
import 'package:flutter_novel/series_list/widget/car_toon_widget.dart';
import 'package:flutter_novel/until/simpleroute_observer.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../config/invoke_manager.dart';
import '../../routers/navigator_utils.dart';

class SeriesListPage extends BasePage {
  const SeriesListPage({super.key});

  @override
  BasePageState<BasePage> getState() => _SeriesListPageState();
}

class _SeriesListPageState extends BasePageState<SeriesListPage> {
  /// 列表控制器
  final ScrollController scrollController = ScrollController();

  /// 刷新组建控制器
  final RefreshController _refreshController = RefreshController(initialRefresh: false);
// 创建全局实例
  final SimpleRouteObserver simpleObserver = SimpleRouteObserver();

  @override
  void initState() {
    super.initState();
    super.appBarTitle = '系列列表';
    print('解决阿金啊姐姐阿九22 ${simpleObserver.pageCount}');
    _request();
  }

  Widget _indication() {
    return IndicationWidget();
  }

  Widget _listBody(BuildContext context, SeriesListState state) {
    if (state.list.isEmpty) return EmptyWidget();
    return BaseGridView(
      enablePullDown: true,
      enablePullUp: true,
      onRefresh: () => _request(),
      onLoading: () => _request(isRefresh: false),
      refreshController: _refreshController,
      scrollController: scrollController,
      data: state.list,
      padding: EdgeInsets.all(10.h),
      childAspectRatio: 0.7,
      crossAxisSpacing: 10.w,
      mainAxisSpacing: 10.h,
      crossAxisCount: 2,
      backgroundColor: const Color(0xFFF3F4F8),
      itemBuilder: (context, index) {
        return CarTonWidget(index: index, model: state.list[index], onTap: () {});
      },
    );
  }

  // 请求数据
  void _request({bool isRefresh = true}) {
    BlocProvider.of<SeriesListBloc>(context).add(GetSeriesListDataEvent(isRefresh));
  }

  void _listener(BuildContext context, SeriesListState state) {
    _refreshController.refreshCompleted();
    _refreshController.loadComplete();
    if (!state.hasNext) {
      _refreshController.loadNoData();
    }
  }

  @override
  Widget buildPage(BuildContext context) {
    return BlocConsumer<SeriesListBloc, SeriesListState>(
      builder: (context, state) {
        if (!state.loadFinish) return SizedBox();
        return Stack(children: [_listBody(context, state), _indication()]);
      },
      listener: _listener,
    );
  }
}
